﻿#region Includes
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
#endregion

namespace GingerGL.Graphics
{
    //------------------------------------------------------------//
    /// <summary>
    /// Texture Manager
    /// <para>Used to manage a list of textures</para>
    /// </summary>
    public class TextureManager
    {
        //--------------------------------------------------------//
        #region Variables
        private Dictionary<string, Texture> mTextures = new Dictionary<string, Texture>();
        #endregion
        //--------------------------------------------------------//
        #region Methods
        /// <summary>
        /// Invalidates all textures
        /// </summary>
        public void Invalidate()
        {
            foreach (KeyValuePair<string, Texture> t in mTextures)
                t.Value.Invalidate();
        }
        /// <summary>
        /// Loads New /Returns EXisting Texture from Image
        /// </summary>
        public Texture FromImage(Image image) { return FromImage(image, false); }           //use parent
        /// <summary>
        /// Loads New /Returns EXisting Texture from Image
        /// </summary>
        public Texture FromImage(Image image, bool clamp)
        {
            if (image == null) return null;                                                 //did you pass a good image
            String name = "Image:" + image.GetHashCode().ToString() + clamp.ToString();     //generate a name
            if (mTextures.ContainsKey(name)) return mTextures[name];                        //check the name, if found return
            Texture texture = new Texture(image, clamp);                                    //create new texture
            mTextures.Add(name, texture);                                                   //add texture to list
            return texture;                                                                 //return texture
        }
        /// <summary>
        /// Creates/Returns Texture based on path.
        /// </summary>
        /// <param name="path">Physicall Path to file</param>
        /// <returns>Texture</returns>
        public Texture GetTexture(string path) { return GetTexture(path, false); }
        /// <summary>
        /// Creates/Returns Texture based on path.
        /// </summary>
        /// <param name="path">Physicall Path to file</param>
        /// <returns>Texture</returns>
        public Texture GetTexture(string path, bool clamp)
        {
            if (mTextures.ContainsKey(path)) return mTextures[path];                        //if exist, return it
            Texture texture = null;                                                         //it doesnt
            if (TextureIsValid(path))                                                       //validate the ext
            {
                FileInfo file = new FileInfo(path);                                         //get file info
                if (file.Exists) texture = new Texture(path, clamp);                        //create texture
            }
            if (texture == null) return null;                                               //dbl check we got something
            mTextures.Add(path, texture);                                                   //add it to list
            return texture;                                                                 //return texture
        }
        /// <summary>
        /// Validates the files extenstion
        /// </summary>
        /// <param name="t">path to file</param>
        /// <returns>TRUE if Valid</returns>
        private bool TextureIsValid(string t)
        {
            if (t == string.Empty) return false;
            string extension = Path.GetExtension(t);
            if (extension != ".png" && extension != ".jpg" && 
                extension != ".jpeg" && extension != ".tiff" && 
                extension != ".tga")
                return false;
            return true;
        }
        #endregion
        //--------------------------------------------------------//
    }
    //------------------------------------------------------------//
}
